<template>
	<el-dialog :title="title" v-model="open" append-to-body :close-on-click-modal="false" width="600px">
		<el-form ref="formRef" :model="form" :rules="rules" label-width="auto">
			<el-row :gutter="10" class="mb20">
				<el-col>
					<el-form-item label="降级方式" prop="type">
						<el-radio-group v-model="form.type">
							<el-radio :label="1">降级到默认经理</el-radio>
							<el-radio :label="0">降级到指定物业管家</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row v-if="!form.type" :gutter="10" class="mb20">
				<el-col>
					<el-form-item label="物业管家" prop="user_agent_id">
						<el-select v-model="form.user_agent_id" placeholder="请选择物业管家" clearable style="width: 100%">
							<el-option v-for="item in userAgentIdOptions" :key="item.id" :label="item.nickname" :value="item.id"> </el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button type="primary" :loading="submitLoading" @click="onSubmitForm"> 确 定 </el-button>
				<el-button @click="onCancel">取 消</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script lang="ts">
import { reactive, ref, toRefs } from 'vue';
// import { selectUsersReplace, usersAgentManagerDemotion, usersManagerDemotion } from '/@/api/admin';
import { ElMessage } from 'element-plus';
export default {
	name: 'updradeDialog',
	components: {},
	setup() {
		const state = reactive({
			title: '经理降级',
			open: false,
			form: {} as any,
			rules: {
				user_agent_id: [{ required: true, message: '物业管家不能为空', trigger: 'blur' }],
			}, // 表单校验
			submitLoading: false,
			userAgentIdOptions: [] as any,
			userAgentIdLoading: false,
		});

		const formRef = ref(null);
		let callback: Function = () => {};

		/********** 点击事件(on开头) **********/
		/** 提交按钮 */
		const onSubmitForm = () => {
			(formRef.value as any)?.validate((valid: boolean) => {
				if (valid) {
					state.submitLoading = true;
					state.form.type ? _usersManagerDemotion(state.form) : _usersAgentManagerDemotion(state.form);
				}
			});
		};
		// 取消按钮
		const onCancel = () => {
			state.submitLoading = false;
			state.open = false;
			reset();
		};

		/********** 方法事件 **********/
		//打开
		const openDialog = (config: any, fn: Function) => {
			console.log(config);
			reset();
			callback = fn;
			state.open = true;
			state.form.user_id = config.user_id;
			_selectUsersReplace({ user_id: state.form.user_id, pageNum: 1, pageSize: 20 });
		};
		// 表单重置
		const reset = () => {
			state.form = {
				type: 1,
				user_agent_id: '',
				user_id: '',
			};
		};

		/********** 网络请求 **********/
		// 查询能更换的用户
		const _selectUsersReplace = (_data: object = {}) => {
			selectUsersReplace(_data)
				.then((res: any) => {
					console.log(res);
					state.userAgentIdOptions = res.data.list;
					state.userAgentIdLoading = false;
				})
				.catch(() => {
					state.userAgentIdLoading = false;
				});
		};
		// 经理降级（经理和代理什么互换）
		const _usersAgentManagerDemotion = (_data: object) => {
			usersAgentManagerDemotion(_data)
				.then((res: any) => {
					console.log(res);
					ElMessage.success('操作成功');
					onCancel();
					callback();
				})
				.catch(() => (state.submitLoading = false));
		};
		// 物业经理降级为普通用户
		const _usersManagerDemotion = (_data: object = {}) => {
			usersManagerDemotion(_data)
				.then(() => {
					ElMessage.success('操作成功');
					onCancel();
					callback();
				})
				.catch(() => (state.submitLoading = false));
		};

		return {
			formRef,
			onSubmitForm,
			openDialog,
			onCancel,
			...toRefs(state),
		};
	},
};
</script>

<style lang="scss" scoped></style>
